package com.lhh.sysadmin.controller;

import com.lhh.sysadmin.entity.R;
import com.lhh.sysadmin.entity.SysUser;
import com.lhh.sysadmin.service.SysUserService;
import com.lhh.sysadmin.utils.JwtUtils;

import com.lhh.sysadmin.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller控制层
 * @author lhh
 * @Date 2024-09-14-39 15:39
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/all")
    public R userList(){
        Map<String,Object> resultMap = new HashMap<>();
        List<SysUser> userList = sysUserService.list();
        resultMap.put("userList",userList);
        return R.ok(resultMap);
    }

    public R userList2(@RequestHeader(required = false) String token){
        if (StringUtil.isNotEmpty(token)){
            Map<String,Object> resultMap = new HashMap<>();
            List<SysUser> userList = sysUserService.list();
            resultMap.put("userList",userList);
            return R.ok(resultMap);
        }else {
            return R.error(401,"没有权限！");
        }
    }

    public R userListNew(@RequestHeader(required = false)String token){
        if (StringUtil.isNotEmpty(token)){
            Map<String,Object> resultMap = new HashMap<>();
            List<SysUser> userList = sysUserService.list();
            resultMap.put("userList",userList);
            return R.ok(resultMap);
        }else {
            return R.error(401,"没有权限访问");
        }
    }

    @RequestMapping("/login")
    public R login()
    {
        String token = JwtUtils.genJwtToken("linhaohan");
        return R.ok().put("token",token);
    }
}
